<?php



namespace Home\Model;

use Think\Model;



/**

* 教师评价表

*/

class TeacherEvaModel extends Model

{

	/**

	 * 查询教师评价

	 * @param  [type] $uid 用户id

	 * @return [type]      评价

	 */

	public function eva($uid){

		$where['uid'] = $uid;

		return $this -> field('star') -> where($where) -> find();

	}



	public function addEva($data){

		return $this -> add($data);

	}

	public function addTeacherEva($data){

		$map['id'] = $data['course_id'];

		//先查出UID

		$uid  = D('Course') ->field('uid') ->where($map) ->find();

		//查询看表里有记录没有

		$res = $this ->where(array('uid' =>$uid['uid'])) ->find();

		//有记录执行下面的

		if($res){

			//把查询出来的转为数组

			$labels = explode(',',$res['labels']);

			//把表单提交上来的转为数组

			$label  = explode(',',$data['labels']);

			//合并数组

			$arr    = array_merge($labels,$label);

			//去除重复的

			$datas  = implode(',',array_unique($arr));

			//去掉前面的","号

			$datas  = ltrim($datas, ",");

			$aaa    = $res['sum'] + 1;

			$xing   = $res['sumstar'] + $data['star'];

			//调用修改方法

			$this   ->taeherSave($uid['uid'],$datas,$aaa,$xing);

		}else{

			//没记录执行一个添加操作

			$res['labels']  = $data['labels'];

			$res['uid']     = $uid['uid'];

			$res['sum']     = $res['sum'] + 1;

			$res['sumstar'] = $res['sumstar'] + $data['star'];

			$this ->create($res);

			$this ->add();

 		}

	}

	/**

	 * [taeherSave description]修改的方法

	 * @param  [type] $id   [description]要该的主键UID

	 * @param  [type] $data [description]要修改的字段

	 * @return [type]       [description]

	 */

	public function taeherSave($id,$data,$aaa,$xing){

		$map['labels']   = $data;

		$map['sum']      = $aaa;

		$map['sumstar']  = $xing;

		$this -> where(array('uid' => $id)) ->save($map);

	}

	/* 获取老师的标签 */

	public function attrLable($data){

		$label = $this ->where(array('uid' =>$data['uid'])) ->find();

		if (!empty($label['labels'])) {

			$data['label'] = explode(',',$label['labels']);

		}

		$data['star'] = $this -> getStar($data['uid']);

		return $data;

	}

	/**

	 * 得到综合评价

	 */

	public function getStar($uid){

		$data = $this ->where(array('uid' =>$uid))->find();

		return intval($data['sumstar'] / $data['sum']);

	}

}